// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function PlayerStateFree(){
	//移动
	hSpeed =lengthdir_x(inputMagnitude*speedWalk,inputDirection);
	vSpeed =lengthdir_y(inputMagnitude*speedWalk,inputDirection);

	//处理碰撞 并移动
	PlayerCollision();

	//攻击逻辑
	if(keyAttack)
	{
		state = PlayerStateAttack;
		stateAttack = AttackSlash;
	}
	//更新精灵下标
	var _oldSprite = sprite_index;
	if(inputMagnitude!=0)
	{
		direction = inputDirection;
		sprite_index = spriteRun;
	} else sprite_index = spriteIdle;
	if(_oldSprite != sprite_index) localFrame = 0; 

	//更新Image_index
	PlayerAnimateSprite();
	
	// 激活键 逻辑
	if(keyActivate)
	{
		
		//1. 检查是否有实体待激活
		//2. 如果没有实体或者实体没有脚本
			//2.a如果拿着实体，扔掉
			//2.b翻滚
		//3. 否则激活实体
		//4. 如果实体是一个NPC，使其面部朝向我们
		
		var _activateX = x + lengthdir_x(10, direction);
		var _activateY = y + lengthdir_y(10, direction);
		var _activateSize = 4;
		var _activateList = ds_list_create();
		activate = noone;
		var _entitiesFound = collision_rectangle_list(
		_activateX - _activateSize,
		_activateY - _activateSize,
		_activateX + _activateSize,
		_activateY + _activateSize,
		pEntity,
		false,
		true,
		_activateList,
		true
		);
		
		//如果列表中第一个对象（_check）是正在拿着的对象或者没有激活脚本，则向下查（查到不可持的可激活对象为止）
		//目的在于排除不可捡起来的植物，查到可以抬起来的对象或者有激活脚本的对象，并赋给activate
		while(_entitiesFound > 0)
		{
			var _check = _activateList[| --_entitiesFound];	
			if((_check != global.iLifted) && (_check.entityActiveScript != -1))
			{
				activate = _check;
				break;
			}
		}
		
		
		
		ds_list_destroy(_activateList);
		
		
		
		//粗略的实体查找方法
		//activate = instance_position(x + _activateX, y + _activateY, pEntity);
		
		if(activate == noone)
		{
			//2.a如果拿着实体，扔掉
			//2.b翻滚
			if(global.iLifted !=noone)
			{
				PlayerThrow();
			}
			else
			{
				state = PlayerStateRoll;
				moveDistanceRemaining = distanceRoll;
			}
			
		}
		else
		{
			//激活实体
			scriptExecuteArray(activate.entityActiveScript, activate. entityActiveArgs);
			
			//使npc面对玩家
			if(activate.entityNPC)
			{
				
				with(activate)
				{
					direction = point_direction(x, y, other.x,other.y);	
					image_index = CARDINAL_DIR;
				}
			}
		}
		
	}
	//掏出道具出来
	if(keyItem) && (!keyActivate) && (global.playerHasAnyItems) && (global.playerEquipped != ITEM.NONE)
	{
		switch(global.playerEquipped)
		{
			case ITEM.BOMB: UseItemBomb();break;	
			case ITEM.BOW: UseItemBow();break;	
			case ITEM.HOOK: UseItemHook();break;	
			default: break;
		}
	}
	//道具循环模式
	if( global.playerHasAnyItems)
	{
		var _cycleDirection = keyItemSelectUp - keyItemSelectDown;
		if(_cycleDirection != 0)
		{
			
			do
			{
				global.playerEquipped += _cycleDirection;
				if(global.playerEquipped < 1) global.playerEquipped = ITEM.TYPE_COUNT - 1;
				if(global.playerEquipped > ITEM.TYPE_COUNT - 1) global.playerEquipped = 1;
				
			}until(global.playerItemUnlocked[global.playerEquipped])
		}
	}
	
}